﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace eCompany.Custom_Controls
{
    public partial class big_shop_ctrl : UserControl
    {
        private eObj.company m_company = null;
        private eObj.currency_list m_cl = null;
        private eObj.shop m_shop = null;

        public bool isCollapsed = false;

        public big_shop_ctrl(eObj.company comp, string shop_cur, eObj.currency_list cl)
        {
            InitializeComponent();

            m_company = comp;
            m_cl = cl;

            eManager.eManager eM = new eManager.eManager();
            m_shop = eM.getShop(m_company, shop_cur);
            namelabelExtended.Locked = true;
            shopValueLabelExtended.Locked = true;
        }

        private void big_shop_ctrl_Load(object sender, EventArgs e)
        {
            namelabelExtended.TextLabel = "Shop " + m_shop.currency;
            shopPictureBox.Image = Tools.GetCurrencyImage(m_shop.currency);

            RefreshShopValues();
            RefreshShopChart();
        }

        public void SetCollapseState(bool bCollapsed)
        {
            if (bCollapsed == true)
            {
                this.Height = 31;
                collapsePB.Image = pics.arrow_collapse;

                isCollapsed = true;
            }
            else
            {
                this.Height = 92;
                collapsePB.Image = pics.arrow_expand;

                isCollapsed = false;
            }
        }

        private void collapsePB_Click(object sender, EventArgs e)
        {
            SetCollapseState(!isCollapsed);
        }

        private void RefreshShopValues()
        {
            eManager.eManager eM = new eManager.eManager();
            double val = eM.getShopStock(m_company, m_shop.currency);
            double val2 = eM.getShopValue(m_shop);
            itemsLbl.Text = eManager.eManager.formatDouble(val) + " items";
            if (val > 0)
            {
                sellItemsCmd.Enabled = true;
                removeAllItemsCmd.Enabled = true;
            }
            else
            {
                sellItemsCmd.Enabled = false;
                removeAllItemsCmd.Enabled = false;
            }
            shopValueLabelExtended.TextExtension = m_shop.currency;
            shopValueLabelExtended.TextLabel = eManager.eManager.formatDouble(val2);
            double gld = eM.currencyToGOLD(m_cl, m_shop.currency, val2);
        }

        private void RefreshShopChart()
        {
            eManager.eManager eM = new eManager.eManager();

            shopChart.Series[0].Points.Clear();
            shopChart.Series[1].Points.Clear();

            List<eManager.ChartItem> lst = eM.getShopSellVolumeChartValue(m_company, m_shop);

            foreach (eCompany.eManager.ChartItem ci in lst)
            {
                shopChart.Series[0].Points.AddXY(ci.date, ci.volume);
            }

            lst = eM.getShopSellChartValue(m_company, m_shop);
            foreach (eCompany.eManager.ChartItem ci in lst)
            {
                shopChart.Series[1].Points.AddXY(ci.date, ci.volume);
            }
        }

        private void shopChart_DoubleClick(object sender, EventArgs e)
        {
            eManager.eManager eM = new eManager.eManager();

            List<eManager.ChartItem> lst1 = eM.getShopSellVolumeChartValue(m_company, m_shop);
            List<eManager.ChartItem> lst2 = eM.getShopSellChartValue(m_company, m_shop);

            eCompany.Forms.big_graph_form dlg = new Forms.big_graph_form(lst1, lst2);
            dlg.ShowDialog();
        }

        [PreEmptive.Attributes.Feature("Shop.AddItems")]
        private void addItemsCmd_Click(object sender, EventArgs e)
        {
            string str_units = "";
            string str_price = "";

            if (Tools.InputBox("Add Items to shop", "Volume", "Price", ref str_units, ref str_price) == DialogResult.OK)
            {
                System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
                str_units = str_units.Replace(",", ".");
                int units = Convert.ToInt32(str_units, culture);
                str_price = str_price.Replace(",", ".");
                double price = double.Parse(str_price, System.Globalization.NumberStyles.Number, culture);

                eCompany.eManager.eManager eM = new eManager.eManager();
                eM.addToShop(m_company, m_shop.currency, units, price);

                RefreshShopValues();
                RefreshShopChart();
            }
        }

        [PreEmptive.Attributes.Feature("Shop.SellItems")]
        private void sellItemsCmd_Click(object sender, EventArgs e)
        {
            string str_units = "";

            if (Tools.InputBox("Sell", "Number of units", ref str_units) == DialogResult.OK)
            {
                double units = Convert.ToDouble(str_units);
                eCompany.eManager.eManager eM = new eManager.eManager();
                eM.sellShop(m_company, m_shop.currency, units);
                RefreshShopValues();
                RefreshShopChart();
            }
        }

        [PreEmptive.Attributes.Feature("Shop.RemoveItems")]
        private void removeAllItemsCmd_Click(object sender, EventArgs e)
        {
            eCompany.eManager.eManager eM = new eManager.eManager();
            eM.removeFromShop(m_company, m_shop.currency);
            RefreshShopValues();
            RefreshShopChart();
        }
    }
}
